const app = getApp();
const utils = require('../../utils/pay.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountbalance: 0, //账户余额
    showlogo: '', //头像
    linkman: '', //昵称
    currId: 0, //当前id
    currUid: 0, //当前用户uid
    firstbrokeragerise: 0, //当前用户等级40-领跑官 60-经销商 80-代理商
    selectId: 0,//1：领跑管，2：经销商，3：代理商
    platform: '',
    money: 0,//升级需要的钱
    message: "",//余额不足时候提示
    isMoney: true,//余额是否充足
    showModel: false,
    inputMoney: 0,
    curfirstbrokerage: 0//操作人的用户等级40-领跑官 60-经销商 80-代理商
  },

  showModel: function () {
    this.setData({ showModel: !this.data.showModel })
  },

  bindInputValue: function (e) {
    this.setData({
      inputMoney: parseFloat(e.detail.value)
    })
  },
  
  checkInput: function () {
    if (!this.checkMoney(this.data.inputMoney)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.setData({
        accountbalance: options.accountbalance, //账户余额
        showlogo: options.showlogo, //头像
        linkman: options.linkman, //昵称
        currId: options.id, //当前id
        currUid: options.useid, //当前用户uid
        firstbrokeragerise: options.firstbrokeragerise,
        curfirstbrokerage: options.curfirstbrokerage
      })
    }
    this.setData({ platform: app.globalData.platform })
  },

  //选择升级
  selectUpdate: function (e) {
    this.setData({ selectId: e.currentTarget.dataset.id })
    this.caculateUpgradeDisMoney();
  },

  //升级分销商为代理商
  upgradeDisUseAccountBalance: function () {
    if (!this.data.selectId) {
      wx.showModal({
        title: '提示',
        content: '请选择合作级别',
        showCancel: false
      })
      return;
    }
    wx.showLoading({
      title: '升级中',
      mask: true
    })
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/UpgradeDisUseAccountBalance',
      data: {
        uid: that.data.currUid,//待升级人UID
        createUid: app.globalData.userInfo.Uid,//操作人UID
        nextLevel: that.data.selectId//1：领跑管，2：经销商，3：代理商
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          wx.showToast({
            title: '成功'
          })
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  //苹果升级申请申请余额代充
  requestAccountBalance: function () {
    wx.showModal({
      title: '提示',
      content: '发送升级申请，保障账户安全！\r\n运营经理会在24小时内联系您',
      confirmText: "立即申请",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '申请中',
            mask: true
          })
          wx.request({
            url: app.globalData.host + '/api/Distributor/RequestAccountBalance?uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              wx.hideLoading();
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '成功'
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //金额验证
  checkMoney: function (obj) {
    if (!obj) {
      return false;
    } else {
      return typeof obj === 'number' && obj % 50 === 0;//是整数，则返回true，否则返回false
    }
  },

  //安卓手机现金直冲
  submitAccountBalanceOrder: function () {
    if (!this.checkMoney(this.data.inputMoney)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
    wx.showLoading({
      title: '充值中',
      mask: true
    })
    wx.request({
      url: app.globalData.host + '/api/Order/SubmitAccountBalanceOrder',
      data: {
        uid: app.globalData.userInfo.Uid,
        count: this.data.inputMoney / 50
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          utils.wxpay(app, res.data.Data, 1)
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 帮好友充值
  toRechargeOthers: function () {
    wx.navigateTo({
      url: '/fenxiao/rechargeOther/index',
    })
  },

  ///获取升级实际扣费
  caculateUpgradeDisMoney: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/CaculateUpgradeDisMoney',
      data: {
        uid: that.data.currUid, //待升级人UID
        createUid: app.globalData.userInfo.Uid,//操作人UID
        nextLevel: that.data.selectId//0：普通用户，1：领跑管，2：经销商，3：代理商
      },
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({ isMoney: true })
        } else {
          that.setData({ isMoney: false })
        }
        that.setData({
          money: res.data.Data,
          message: res.data.Message
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.selectId) {
      this.caculateUpgradeDisMoney();
    }
  }


})